# -*- coding:utf-8 -*-
import time

from selenium.webdriver.common.by import By

from PageClass.basePage import BasePage
from Util import logger, config, ZTCCLOUDError


# 业务报账系统
class BasIndexPage(BasePage):

    def __init__(self, driver):
        BasePage.__init__(self, driver)

    def open_boe(self, boeType):
        """
            业务报账打开单据并进入填单页面
        :param boeType: 单据名称
        :return:
        """
        conf = config.basMenuConf
        areaType = conf.get(boeType).boeClassify
        if not areaType:
            raise ZTCCLOUDError(f'业务报账不存在名称为：{boeType} 的单据，请确认')
        logger.info(f"待打开的单据类型为：{areaType} 区域中的 {boeType}")
        time.sleep(3)
        self.selectTabType(areaType)
        self.boeEntry(boeType)
        self.switchWindow()

    # 选择业务报账类型
    _basMenuType = (By.XPATH, '//li[@role="menuitem"]/*[@class="el-badge item"]')

    def selectTabType(self, tabType):
        for i in self.find_elements(*self._basMenuType):
            if tabType in i.text:
                self._clickElement(i)
                logger.info(f'点击进入{tabType}界面')
                break

    # 我的单据
    _myBill = (By.ID, 'tab-myBill')

    def click_myBill(self):
        self.click(*self._myBill)
        logger.info('点击我的单据')

    # 我的草稿
    _myDraft = (By.ID, 'tab-myDraft')

    def click_myDraft(self):
        self.click(*self._myDraft)
        logger.info('点击我的草稿')

    # 待我处理
    _waitDeal = (By.ID, 'tab-waitDeal')

    def click_waitDeal(self):
        self.click(*self._waitDeal)
        logger.info('点击待我处理')

    # 我的付款
    _payFailed = (By.ID, 'tab-payFailed')

    def click_payFailed(self):
        self.click(*self._payFailed)
        logger.info('点击我的付款')

    # 更多
    _more = (By.CLASS_NAME, 'viewMore')

    def click_more(self):
        self.find_element(*self._more).click()
        logger.info('点击更多按钮')

    # 首页
    _homePage = (By.XPATH, '//span[@role="link" and contains(text(), "首页")]')

    def backToHomePage(self):
        self.click(*self._homePage)
        logger.info('回到首页')

    # 单据编号
    _boeNumQuery = (By.ID, 'undefined_boeNo')

    def input_boeNumQuery(self, text):
        self.send_text(text, *self._boeNumQuery)

    # 查询
    def click_queryButton(self):
        self.click_button('查询')
        logger.info('点击查询按钮')

    # 校验boeNum是否存在
    def checkBoeNumExistIsOrNot(self, boeNum):
        if self.find_element(*(By.XPATH, '//*[@id="app"]//table/tbody/tr/td[2]/div/button')).text.strip(" ") == boeNum:
            logger.info(f'{boeNum} -> 单据存在')
            return True
        else:
            logger.info(f'{boeNum} -> 单据不存在')
            return False

    # 单据入口
    def boeEntry(self, boeType):
        for i in self.find_elements(*(By.CLASS_NAME, 'boe')):
            if i.text.strip(" ") == boeType:
                self._clickElement(i)
                logger.info(f'进入单据 -> {boeType}')
                break
